/*
 * PhotoSetGUI.java
 *
 * Created on April 3, 2007, 1:03 AM
 */

package edu.byu.isys413.group1E.gui;

import edu.byu.isys413.group1E.*;
import edu.byu.isys413.group1E.data.*;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author  KC Kern
 */
public class PhotoSetGUI extends javax.swing.JFrame {
    
    PrintHandler ph = new PrintHandler();
    ArrayList<BusinessObject> psList = ph.listPhotoSets();
    int selection;
    
    /** Creates new form PhotoSetGUI */
    public PhotoSetGUI() throws DataException, SQLException, ConnectionPoolException {
        initComponents();
        
        this.populatePSList();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        selectLabel = new javax.swing.JLabel();
        addLabel = new javax.swing.JLabel();
        numLabel = new javax.swing.JLabel();
        descrLabel = new javax.swing.JLabel();
        jList1 = new javax.swing.JList();
        save = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        psJList = new javax.swing.JList();
        no = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        descr = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        selectLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        selectLabel.setText("Select a Photo Set to Edit");

        addLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        addLabel.setText("or Add a New Photo Set");

        numLabel.setText("Number of Photos in this Set:");

        descrLabel.setText("Photo Set Description");

        save.setText("Create");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveCreate(evt);
            }
        });

        psJList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                selectSet(evt);
            }
        });

        jScrollPane1.setViewportView(psJList);

        descr.setColumns(20);
        descr.setRows(5);
        jScrollPane2.setViewportView(descr);

        jButton1.setText("Close");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(selectLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jList1)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(numLabel)
                        .addComponent(addLabel)
                        .addComponent(no, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE))
                    .addComponent(descrLabel)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(save)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1))
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selectLabel)
                    .addComponent(addLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(numLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(no, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                                .addComponent(descrLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addComponent(jList1)
                                .addGap(148, 148, 148)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(save)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void close(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close
this.setVisible(false);// TODO add your handling code here:
    }//GEN-LAST:event_close
    
    private void SaveCreate(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveCreate
        if (save.getText().equals("Create")) {
            try {
                int nis = Integer.parseInt(no.getText());
                ph.makePhotoSet(descr.getText(), nis);
                try {
                    psList = ph.listPhotoSets();
                    this.populatePSList();
                } catch (ConnectionPoolException ex) {
                    ex.printStackTrace();
                } catch (DataException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            } catch (Exception ex) {  }
        }else {
            try {
                
                int nis = Integer.parseInt(no.getText());
                ph.editPhotoSet(psList.get(selection).getId(), descr.getText(), nis);
                this.populatePSList();
                
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        
        try {
            psList = ph.listPhotoSets();
            this.populatePSList();
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        } catch (DataException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_SaveCreate
    
    private void selectSet(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_selectSet
        selection = psJList.getSelectedIndex();
        try {
            psList = ph.listPhotoSets();
            save.setText("Save");
            PhotoSetBO psBO =  (PhotoSetBO)psList.get(psJList.getSelectedIndex());
            no.setText(psBO.getNuminSet()+"");
            descr.setText(psBO.getDescription());
            
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (DataException ex) {
            ex.printStackTrace();
        }
        
        try {
            
            this.populatePSList();
        } catch (DataException ex) {
            ex.printStackTrace();
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
    }//GEN-LAST:event_selectSet
    
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                try {
//                    new PhotoSetGUI().setVisible(true);
//                } catch (SQLException ex) {
//                    ex.printStackTrace();
//                } catch (ConnectionPoolException ex) {
//                    ex.printStackTrace();
//                } catch (DataException ex) {
//                    ex.printStackTrace();
//                }
//            }
//        });
//    }
    
    public void populatePSList() throws DataException, SQLException, ConnectionPoolException {
       
        String[] list = ph.getPSDesc();
        this.psJList.setListData(list);

    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel addLabel;
    private javax.swing.JTextArea descr;
    private javax.swing.JLabel descrLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField no;
    private javax.swing.JLabel numLabel;
    private javax.swing.JList psJList;
    private javax.swing.JButton save;
    private javax.swing.JLabel selectLabel;
    // End of variables declaration//GEN-END:variables
    
}
